package com.study.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletContext;
import java.util.Map;

@RestController
public class IndexController {

    @Autowired
    private ServletContext servletContext;

    @RequestMapping("/index")
    public String index(){
        Map<String, String> userinfo = (Map<String, String>) servletContext.getAttribute("userinfo");
        System.out.println(userinfo.get("1001"));
        return "hello";
    }
}

/*
获取ServletContext 的几种方法：
1 通过HttpServletRequest request获取: ServletContext sc = request.getServletContext();
2 通过自动注入获取，该方法可以在@Controller下使用，暂未在Service中测试
    @Autowired
    private ServletContext servletContext;
3. 通过WebApplicationContext获取
    ServletContext servletContext = webApplicationConnect.getServletContext();
 */